package org.game.module.taskbo;

import java.util.List;
import java.util.Map;

import org.cfg.TaskCfg;
import org.constant.EventType;
import org.constant.TaskConstant;
import org.error.GameErrorCode;
import org.game.constant.SessionConstent;
import org.game.db.bean.Player;
import org.game.db.bean.Task;
import org.game.db.cache.TaskCache;
import org.game.db.proxy.bean.PlayerDataProxy;
import org.game.log.builder.LogDsp;
import org.game.log.dsp.GoodsDsp;
import org.game.manager.ItemManager;
import org.game.manager.TaskManager;
import org.game.msgBuilder.TaskMsgBuilder;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventPlugin;
import org.game.protobuf.c2s.C2STaskMsg.ReqTasks;
import org.game.protobuf.c2s.C2STaskMsg.SubmitTask;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;
import org.game.protobuf.s2c.S2CShareMsg.RewardsMsg;
import org.game.protobuf.s2c.S2CTaskMsg.ResultSubmitTask;
import org.game.protobuf.s2c.S2CTaskMsg.TaskInfo;
import org.game.util.ProtoUnit;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.tools.ToolError;

/**
 * @author : DengYing
 * @CreateDate : 2018年1月9日 下午3:06:26
 * @Description ：Please describe this document
 */
public class TaskBOImpl implements TaskBO {

	@Override
	public byte[] getTasks(ISession session, ReqTasks msg) throws AbstractLogicModelException {
		Player player = session.getAttr(SessionConstent.PLAYER);
		return ProtoUnit.toByte(TaskMsgBuilder.createRespTasks(player,ChangeType.DEFAULT));
	}

	@Override
	public byte[] submitTasks(ISession session, SubmitTask msg) throws AbstractLogicModelException {
		
		Player player = session.getAttr(SessionConstent.PLAYER);
		
		Task task = TaskCache.me().getByPlayerIdCid(player.getId(), msg.getTaskCid());
		
		
		
		// 状态不对
		ToolError.isAndTrue(GameErrorCode.TASK_STATUS_ERROR, task.getStatus()!=TaskConstant.STATUS_FINISH);
		
		TaskCfg cfg = task.getTaskProxy().getCfg();
		LogDsp logDsp = LogDsp.newBuilder(GoodsDsp.SUBMIT_TASK).addAttr("cid", cfg.getId());
		List<RewardsMsg> rewards = ItemManager.unpackNotify(player, cfg.getReward(),logDsp);
		task.setStatus(TaskConstant.STATUS_RECEIVE);
		
		// 不重置的任务提交后就删除
		if (cfg.getResetType() == TaskConstant.RESET_NO) {
			task.delete();	
			
			// 只有删除的才单独记录完成
			PlayerDataProxy pdProxy = player.getPlayerDatasFkPlayerId().getPlayerDataProxy();
			pdProxy.getFinishTasks().put(task.getCid(), task.getProgress());
			pdProxy.save();
		}else {
			task.update();
		}
		


		


		
		
		List<TaskInfo> taskInfos = Lists.newArrayList();
		taskInfos.add(TaskMsgBuilder.createTaskInfo(player, ChangeType.UPDATE, task));
		TaskManager.me().notifyTaskInfos(player, taskInfos);
		
		// 触发任务激活事件
		Map<String, Object> in = Maps.newHashMap();
		GameEvent event = new GameEvent(player, EventType.TASK_ACCEPT, in);
		GameEventPlugin.syncSubmit(event);
		
		ResultSubmitTask.Builder builder = ResultSubmitTask.newBuilder();
		builder.setTaskCid(task.getCid());
		builder.setTaskDbId(String.valueOf(task.getId()));
		builder.addAllRewards(rewards);
		return ProtoUnit.toByte(builder.build());
	}
}
